//
//  TestCoreTextView2.m
//  BookTest1
//
//  Created by IOS3 on 2018/10/31.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "TestCoreTextView2.h"
#import <CoreText/CoreText.h>

@implementation TestCoreTextView2



- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    // 获取当前 context
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 转换坐标
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    CGMutablePathRef path = CGPathCreateMutable();
    // CGPathAddRect(path, NULL, self.bounds);
    CGPathAddEllipseInRect(path, NULL, self.bounds);
    
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:@"Hello World"];
    attr = [[NSAttributedString alloc] initWithString:@"Hello World! "
                                     " 创建绘制的区域，CoreText 本身支持各种文字排版的区域，"
                                     " 我们这里简单地将 UIView 的整个界面作为排版的区域。"
                                     " 为了加深理解，建议读者将该步骤的代码替换成如下代码，"
                                     " 测试设置不同的绘制区域带来的界面变化。"];
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attr);
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attr.length), path, NULL);
    
    CTFrameDraw(frame, context);
    
    CFRelease(frame);
    CFRelease(path);
    CFRelease(framesetter);
    
}


@end
